﻿/**
 * ...
 * @author Sergey Mamyan
 */


package tx.squirrel.neko.core.model;

import neko.db.Object;

class Friend extends Object {
	
	static var TABLE_IDS = ["uid1", "uid2"];
	public static var manager = new FriendManager();
	
	public var uid1:String;
	public var uid2:String;
	
	public static function add(uid1:String, uid2:String):Friend {
		if (uid1 > uid2) {
			var tmp:String = uid1;
			uid1 = uid2;
			uid2 = tmp;
		}
		var friendship:Friend = manager.getWithKeys( { uid1:uid1, uid2:uid2 } );
		if (friendship == null) {
			friendship = new Friend();
			friendship.uid1 = uid1;
			friendship.uid2 = uid2;
			friendship.insert();
		}
		return friendship;
	}
	
	public static function clear(uid:String):Void {
		manager.clear(uid);
	}
	
	public function new() {
		super();
	}
	
}

class FriendManager extends neko.db.Manager < Friend > {
	
	public function new () {
		super(Friend);
	}
	public function clear(uid:String):Void {
		execute("DELETE FROM " + table_name + " WHERE uid1=" + uid + " OR uid2=" + uid);
	}
}